﻿using BZ.Camera.MindVision;
using HalconDotNet;
using System.Collections.Generic;
using System.Linq;

namespace BZ.AcqDevice.MindVision
{
    /// <summary>
    /// 此类 用于调试及初始化，正常测量请获取相应的对象
    /// </summary>
    public class MVControllerStrategy
    {
        bool _isConnected = false;
        /// <summary>
        /// 用于判断此类的相机是否已初始化
        /// </summary>
        public bool IsConnected
        {
            get
            {
                return _isConnected;
            }
            set
            {
                if (_isConnected != value)
                {
                    _isConnected = value;
                }
            }
        }
        //相机名,相机的实例对象
        private static Dictionary<string, MVController> camList = new Dictionary<string, MVController>();
        /// <summary>
        /// 防止手动和自动调用，互相影响，导致类的对象异常
        /// </summary>
        public void Clear()
        {
            camList.Clear();
        }
        /// <summary>
        /// 实例化需要使用的对象
        /// </summary>
        public void NewObject(string objName)
        {
            if (!camList.ContainsKey(objName))
            {
                camList.Add(objName, new MVController());
            }
        }

        /// <summary>
        /// 用于获取对象
        /// </summary>
        /// <param name="camName">相机名</param>
        /// <returns></returns>
        public MVController getObject(string camName)
        {
            if (camList.ContainsKey(camName))
            {
                return camList[camName];
            }
            else
            {
                return null;
            }
        }
        public bool LoadConfig(string camName, string path)
        {
            bool ret = false;
            try
            {
                ret = getObject(camName).LoadParameterFromFile(path);
            }
            catch
            {
                return false;
            }
            return ret;
        }
        /// <summary>
        /// 迈德威视相机初始化
        /// </summary>
        /// <param name="camName">相机名</param>
        /// <param name="sn">相机序列号</param>
        /// <param name="mode">触发模式：0-连续模式，1-软触发，2-硬触发</param>
        /// <returns></returns>
        public bool Connect(string camName, string sn, int mode)
        {
            bool ret = false;
            try
            {
                ret = getObject(camName).Connect(sn, mode);
            }
            catch
            {
                return false;
            }
            return ret;
        }
        /// <summary>
        /// 相机触发
        /// </summary>
        /// <param name="camName">相机名</param>
        /// <returns>触发结果</returns>
        public bool Snap(string camName)
        {
            return getObject(camName).Snap();
        }

        /// <summary>
        /// 获取图像
        /// </summary>
        /// <param name="camName">相机名</param>
        /// <returns>获取结果</returns>
        public HImage Image(string camName, int timeOut = 3000)
        {
            return getObject(camName).Image(timeOut);
        }

        //public Bitmap ImageBitmap(string camName, int timeOut = 3000)  //!!
        //{
        //    return getObject(camName).ImageBitmap(timeOut);
        //}
        /// <summary>
        /// 查看相机的对象
        /// </summary>
        /// <returns>相机个数集合</returns>
        public int getCount()
        {
            return camList.Count();
        }
        /// <summary>
        /// 相机触发
        /// </summary>
        /// <param name="camName">相机名</param>
        /// <returns>触发结果</returns>
        public bool Close(string camName)
        {
            return getObject(camName).Close();
        }
        public int Count()
        {
            return camList.Count();
        }
    }
}
